{% extends 'accounts/base.html' %}

{% load form_extras %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">{{ title }}</h1>
</div>

<div class="row">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title mb-0">
                    <i class="fas fa-user"></i> {{ title }}
                </h5>
            </div>
            <div class="card-body">
                <form method="post">
                    {% csrf_token %}
                    
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.username.id_for_label }}" class="form-label">用户名</label>
                                {{ form.username|add_class:"form-control" }}
                                {% if form.username.errors %}
                                    <div class="text-danger">{{ form.username.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                        
                        {% if form.nickname %}
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.nickname.id_for_label }}" class="form-label">昵称</label>
                                {{ form.nickname|add_class:"form-control" }}
                                {% if form.nickname.errors %}
                                    <div class="text-danger">{{ form.nickname.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                        {% endif %}
                    </div>
                    
                    <div class="row">
                        {% if form.email %}
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.email.id_for_label }}" class="form-label">邮箱</label>
                                {{ form.email|add_class:"form-control" }}
                                {% if form.email.errors %}
                                    <div class="text-danger">{{ form.email.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                        {% endif %}
                        
                        {% if form.phone %}
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.phone.id_for_label }}" class="form-label">电话</label>
                                {{ form.phone|add_class:"form-control" }}
                                {% if form.phone.errors %}
                                    <div class="text-danger">{{ form.phone.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                        {% endif %}
                    </div>
                    
                    {% if form.password1 %}
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.password1.id_for_label }}" class="form-label">新密码</label>
                                {{ form.password1 }}
                                {% if form.password1.errors %}
                                    <div class="text-danger">{{ form.password1.errors }}</div>
                                {% endif %}
                                {% if form.password1.help_text %}
                                    <div class="form-text">{{ form.password1.help_text|safe }}</div>
                                {% endif %}
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.password2.id_for_label }}" class="form-label">确认新密码</label>
                                {{ form.password2 }}
                                {% if form.password2.errors %}
                                    <div class="text-danger">{{ form.password2.errors }}</div>
                                {% endif %}
                                {% if form.password2.help_text %}
                                    <div class="form-text">{{ form.password2.help_text }}</div>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                    {% endif %}
                    
                    <div class="row">
                        {% if form.is_active %}
                        <div class="col-md-6">
                            <div class="mb-3">
                                <div class="form-check">
                                    {{ form.is_active|add_class:"form-check-input" }}
                                    <label for="{{ form.is_active.id_for_label }}" class="form-check-label">是否启用</label>
                                </div>
                                {% if form.is_active.errors %}
                                    <div class="text-danger">{{ form.is_active.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                        {% endif %}
                        
                        {% if form.roles %}
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">角色</label>
                                <div class="row">
                                    {% for choice in form.roles.field.choices %}
                                    <div class="col-md-12">
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox" 
                                                   name="roles" 
                                                   value="{{ choice.0 }}"
                                                   id="id_roles_{{ choice.0 }}"
                                                   {% if choice.0 in form.roles.value %}checked{% endif %}>
                                            <label class="form-check-label" for="id_roles_{{ choice.0 }}">
                                                {{ choice.1 }}
                                            </label>
                                        </div>
                                    </div>
                                    {% endfor %}
                                </div>
                                {% if form.roles.errors %}
                                    <div class="text-danger">{{ form.roles.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                        {% endif %}
                    </div>
                    
                    <div class="mb-3">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save"></i> 保存
                        </button>
                        {% if not is_profile_page %}
                        <a href="{% url 'accounts:user_list' %}" class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i> 返回
                        </a>
                        {% endif %}
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}